{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "with open(\"股票代码_企业名称.csv\", 'r', encoding='utf-8') as f:\n",
    "    lines = f.read().split(\"\\n\")\n",
    "code_name_dict = {line.split(\",\")[0]: line.split(\",\")[1] for line in lines}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from shutil import copyfile\n",
    "source_dir = \"D:\\ALL\\项目\\碳信息披露\\测试pdf\\所有测试PDF\\\\\"\n",
    "target_dir = \"D:\\ALL\\项目\\碳信息披露\\测试pdf\\所有测试PDF_new_name\\\\\"\n",
    "# 重命名成 股票代码_公司名_年份.pdf\n",
    "for file in os.listdir(source_dir):\n",
    "    code = file.split(\"-\")[0]\n",
    "    year = file.split(\"-\")[1]\n",
    "    try:\n",
    "        name = code_name_dict[code]\n",
    "    except:\n",
    "        name = \"未知\"\n",
    "        print(f\"未知公司：{code}\")\n",
    "    new_name = f\"{code}_{name}_{year}.pdf\"\n",
    "    copyfile(source_dir + file, target_dir + new_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "filename_list = []\n",
    "for file in os.listdir(source_dir):\n",
    "    code = file.split(\"-\")[0]\n",
    "    year = file.split(\"-\")[1]\n",
    "    try:\n",
    "        name = code_name_dict[code]\n",
    "    except:\n",
    "        name = \"未知\"\n",
    "        print(f\"未知公司：{code}\")\n",
    "    filename_list.append(f\"{code}_{name}_{year}.pdf\")\n",
    "\n",
    "new_filename_list = [file for file in os.listdir(target_dir)]\n",
    "# 找出在 filename_list中，不在new_filename_list中的文件\n",
    "for file in filename_list:\n",
    "    if file not in new_filename_list:\n",
    "        print(file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 找出filename_list中重复的文件\n",
    "from collections import Counter\n",
    "c = Counter(filename_list)\n",
    "for k, v in c.items():\n",
    "    if v > 1:\n",
    "        print(k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target_dir = \"D:\\ALL\\项目\\碳信息披露\\测试pdf\\所有测试PDF_new_name\\\\\"\n",
    "new_filename_list = [file for file in os.listdir(target_dir)]\n",
    "for file in new_filename_list:\n",
    "    print(file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)]"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "11938c6bc6919ae2720b4d5011047913343b08a43b18698fd82dedb0d4417594"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
